package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product/product")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class ProductController {

    @Autowired
    private ProductService productService;


    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Product>> getProductPageList(@PathVariable Integer page, @PathVariable Integer limit
        , ProductDto productDto){
        PageInfo<Product> pageInfo = productService.getProductPageList(page,limit,productDto);
        return Result.ok(pageInfo);
    }

    @PostMapping("/save")
    public Result saveProduct(@RequestBody Product product){
        productService.saveProduct(product);
        return Result.ok(null);
    }
    //http://127.0.0.1:4523/m1/5520511-5196902-default/admin/product/product/getById/1
    @GetMapping("/getById/{productId}")
    public Result<Product> getById(@PathVariable Long productId){
        Product product = productService.getById(productId);
        return Result.ok(product);
    }

    /**
     * 更改商品审核状态.
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateAuditStatus/{id}/{status}")
    public Result updateAuditStatus(@PathVariable Long id,@PathVariable Integer status){
        productService.updateAuditStatus(id,status);
        return Result.ok(null);
    }

    /**
     * 更改商品上下架状态.
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
        productService.updateStatus(id,status);
        return Result.ok(null);
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        productService.deleteById(id);
        return Result.ok(null);
    }

    @PutMapping("/updateById")
    public Result updateById(@RequestBody Product product){
        productService.updateById(product);
        return Result.ok(null);
    }
}
